using System;
using System.Collections.Generic;
using System.Text;

namespace Common
{
    /// <summary>
    /// 
    /// </summary>
    public class IntUtil
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static int[] ToArray(string input)
        {
            List<int> parts = new List<int>();
            if (!string.IsNullOrEmpty(input))
            {
                string[] strParts = StringUtil.Split(input);
                if (strParts != null && strParts.Length > 0)
                {
                    try
                    {
                        for (int i = 0; i < strParts.Length; i++)
                        {
                            parts.Add(int.Parse(strParts[i]));
                        }
                    }
                    catch
                    {
                        parts = new List<int>();
                    }
                }
            }
            if (parts.Count > 0)
            {
                return parts.ToArray();
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="parts"></param>
        /// <returns></returns>
        public static string ToString(int[] parts)
        {
            string output = string.Empty;
            if (parts != null && parts.Length > 0)
            {
                for (int i = 0; i < parts.Length; i++)
                {
                    output += parts[i].ToString();
                    if (i < parts.Length - 1)
                    {
                        output += ",";
                    }
                }
            }
            return output;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="intValsA"></param>
        /// <param name="intValsB"></param>
        /// <returns></returns>
        public static bool Intercept(List<int> intValsA, List<int> intValsB)
        {
            if (intValsA == null || intValsA.Count == 0 || intValsB == null || intValsB.Count == 0)
                return false;
            foreach(int a in intValsA)
            {
                foreach(int b in intValsB)
                {
                    if (a == b)
                        return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="intVals1"></param>
        /// <param name="intVals2"></param>
        /// <returns></returns>
        public static List<int> Intersection(List<int> intVals1, List<int> intVals2)
        {
            List<int> sharedInts=new List<int>();
            if(intVals1 !=null && intVals2 !=null && intVals1.Count>0 && intVals2.Count>0)
            {
                foreach(int intVal in intVals1)
                {
                    if(intVals2.Contains(intVal))
                    {
                        sharedInts.Add(intVal);
                    }
                }
            }
            return sharedInts;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="intVector"></param>
        /// <returns></returns>
        public static int Minimum(List<int> intVector)
        {
            if(intVector==null || intVector.Count==0)
                throw new Exception("Input is empty");
            int minimum = int.MaxValue;
            foreach(int intval in intVector)
            {
                if(intval<minimum)
                    minimum = intval;
            }
            return minimum;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="intVector"></param>
        /// <returns></returns>
        public static int Maximum(List<int> intVector)
        {
            if (intVector == null || intVector.Count == 0)
                throw new Exception("Input is empty");
            int maximum = int.MinValue;
            foreach (int intval in intVector)
            {
                if (intval > maximum)
                    maximum = intval;
            }
            return maximum;
        }
    }
}
